<?php
//error_reporting('0');
define("MCTDESIGN",true);
include('./_inc/connect.php');
include('./_inc/functions.php');
require_once('./_classes/_classes.inc.php');
$temp = new Smarty_McT();
$temp->smarty_site();
include './_classes/mailer/senmail.php';
global $anyDB,$tb_fix;
$ser = $_GET['ser'];
switch($ser){
    case "print":
	       echo show_print_page();
       break;
   case "email":
           echo send_email_page();
       break;   
   case "getnametour":
           echo getnametour();
       break;    
   default : 
	       echo "cut";
	   break;     
}
function getnametour(){
    global $temp,$anyDB,$tb_fix;
    $id = (int)$_GET['tourid'];
    $sql = "select tou_id,tou_title from ".$tb_fix."_tour where tou_id = '$id' limit 0,1";
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext($q);
    $html = "<input type='hidden' value='".$id."'  id='idtour' /> <b style='color:#008fd3'>".$r['tou_title']."</b> ".$gia." ";
    echo $html;
    exit();
}
function show_print_page(){
    global $temp,$anyDB,$tb_fix;
    $html_url = get_data('shop_url','config_values');
    $temp->assign( 'htmlURL', "".$html_url."" );
    $dir_tpl = temp_default();
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    $id = (int)$_GET['id'];
    $sql = "select * from ".$tb_fix."_tour where tou_id = '$id' limit 0,1";
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext($q);
    $r['ml_name']=get_data('shop_name','config_values');
    $r['ml_add']=get_data('shop_address','config_values');
    $r['ml_fax']=get_data('shop_fax','config_values');
    $r['ml_mail']=get_data('shop_mail','config_values');
    $r['ml_phone']=get_data('shop_phone','config_values');
    $temp->assign('row',$r);
    $content = $temp->fetch("modules/print.mct");
    return $content;
}
function send_email_page(){
     global $temp,$anyDB,$tb_fix;
     $html_url = get_data('shop_url','config_values');
    $temp->assign( 'htmlURL', "".$html_url."" );
    $dir_tpl = temp_default();
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    $id = (int)$_GET['id'];
    $sql = "select * from ".$tb_fix."_tour where tou_id = '$id' limit 0,1";
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext($q);
    $temp->assign('row',$r);
    $content = $temp->fetch("modules/email.mct");
    return $content;
}
// send tour
if(isset($_POST['frm_send_mail']) && isset($_POST['flag']) && $_POST['flag'] == 1){
    $touId = (int)$_POST['tour_id'];
    $fname = addslashes($_POST['f_name']);
    $femail = addslashes($_POST['f_email']);
    $temail = addslashes($_POST['t_email']);
    $tsubject = addslashes($_POST['t_subject']);
    $tmes = addslashes($_POST['t_message']);
    
    if(!empty($femail) || !empty($temail) || !empty($tsubject) ||!empty($tmes)){
        $sql = "select t.*,tm.tme_id,tm.tme_title as cat_title
                    from ".$tb_fix."_tour as t
                            left join ".$tb_fix."_tour_menu as tm
                                on tm.tme_id = t.tou_category    
                    where tou_id = '$touId' limit 0,1";
                  //  echo $sql;
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $touId = $r['tou_id'];
        $tou_tit = $r['tou_title'];
        $rootid = $r['tou_root_cat_id'];
        $tou_cat_id = $r['tou_category'];
        $r['tou_cat_name'] = convertText($r['cat_title']);
        $r['touTileURL'] = convertText($r['tou_title']);
        $r['mes'] = $tmes;
        $sql = "select mti_url from ".$tb_fix."_tour_img where tou_id = $touId";
        $q = $anyDB->query($sql);
        $rIMG = $anyDB->getNext($q);
        $r['tou_image'] = $rIMG['mti_url'];
        if($rootid == 1){
            $r['rootName'] = 'tour-nuoc-ngoai';
        }else{
            $r['rootName'] = 'tour-trong-nuoc';
        }
        if(!empty($fname)){
            $r['fmes'] = " tên là: <b>$fname</b>, hòm thư: <b>$femail</b>";
        }else{
            $fname = "Email từ website du lịch Mạnh Long";
            $r['fmes'] = " hòm thư là <b>$femail</b>";
        }
        $r['weblink'] = get_data('shop_url','config_values');
        $temp->assign("row",$r);
        $newMes = $temp->fetch('modules/email_template.mct');
        $send = send_tour_to_friend($fname,$femail,$temail,$tsubject,$newMes);
        if($send){
            echo "<script>alert('Bài viết đã được gửi đi. cảm ơn sự quan tâm của bạn !');window.parent.close()</script>";
        }else{
            echo "<script>alert('Có lỗi xảy ra vui lòng thử lại');window.parent.close()</script>";
        }
    }
}
?>